package com.fit.pages.registration;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.registration.RegistrationDAO;
import com.fit.entities.Registration;

@UserAccess
@AdminAccess
public class EditRenewal {

	@InjectPage
	private ManageRegistrations manage;

	@Property
	@SessionState(create = false)
	private Registration registration;

	@Inject
	private RegistrationDAO registrationDAO;

	Object onActivate(String id) {
		manage.setError("");
		manage.setConfirmation("");
		Long l = new Long(id);
		Registration r = registrationDAO.findByID(l);
		if (r != null) {
			registration = r;
		}
		if (registration == null) {
			manage.setError("Error");
			return manage;
		}
		return null;
	}

	Object onSubmitFromCreateForm() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			registrationDAO.update(registration);
			manage.setConfirmation("Successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}

	}

}
